package cn.lccabc.dichotomy.no0162;

public class Solution {

    public static int findPeakElement(int[] nums) {
        if (nums.length == 1) return 0;
        if (nums.length == 2){
            return nums[0] > nums[1] ? 0 : 1;
        }
        int left = 0, right = nums.length - 1;
        while (left <= right){
            int mid = left + (right - left) / 2;
            if (nums[mid] > nums[mid - 1] && nums[mid] > nums[mid + 1]){
                return mid;
            } else if (nums[mid] < nums[mid - 1]){
                right = mid - 1;
            } else {
                left = mid + 1;
            }
        }
        return -1;
    }


    public static void main(String[] args) {
        int[] nums = new int[]{1,2,1,3,5,6,4};
        int result = findPeakElement(nums);
        System.out.println(result);
    }
}
